layui.use(['form', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form,
    fsoft = layui.fsoft;
  var id = $('#id').val();
  form.render();
  rebulidActionUrl({ value: form.val('form-menu').menuType });
  //上级菜单
  $.post(layui.cache['contentPath'] + "/sys-menu/findList", { menuTypeNotIn: 2 }, function (data) {
    data = eval('(' + data + ')');
    if (data.code == "0") {
      var selectMenu = xmSelect.render({
        el: '#xm-select-menuTrees', name: "parentId",
        initValue: [$('#parentId').val()],
        prop: { name: 'name', value: 'id' },
        filterable: true, clickClose: true, radio: true,
        data: data.data, paging: true, pageSize: 5,
        template({ item, sels, name, value }) {
          return item.name + '<span class="layui-word-aux" style="position: absolute; right: 10px;">' + item.parentName + '</span>';
        },
        on: function (data) {
          var arr = data.arr;
          if (data.isAdd) {
            $('#parentId').val(arr[0].id);
          } else {
            $('#parentId').val("");
          }
        }
      });
    }
  });
  //图标下拉列表
  $.post(layui.cache['contentPath'] + "/icons/findList", { status: 1 }, function (data) {
    data = eval('(' + data + ')');
    if (data.code == "0") {
      xmSelect.render({
        el: '#xm-select-icons'
        , name: "icon"
        , initValue: [$('#icon').val()]
        , prop: { name: 'className', value: 'className' }
        , filterable: true
        , clickClose: true
        , radio: true
        , data: data.data, paging: true, pageSize: 10
        , model: {
          label: {
            block: {
              template: function (item, sels) {
                return '<i class="' + item.className + '" aria-hidden="true"></i>';
              }
            }
          }
        },
        template({ item, sels, name, value }) {
          var icons = new Array();
          icons.push('<span>');
          if (item.iconGroup == 'layui-icon')
            icons.push('<i class="' + item.code + ' ' + item.className + '"></i>');
          else
            icons.push('<i class="' + item.className + '" aria-hidden="true"></i>');
          icons.push(" " + item.name);
          icons.push('</span>');
          return icons.join('');
        }
      });
    }
  });
  // 监听提交
  form.on('submit(save)', function (data) {
    var submitUrl = layui.cache['contentPath'] + '/sys-menu/save';
    if (id != undefined && id != "") {
      submitUrl = layui.cache['contentPath'] + '/sys-menu/modify';
    }
    fsoft.post(submitUrl, data.field, function (rs) {
      if (rs.code == 0) {
        fsoft.successMsg((id != undefined && id != "") ? "编辑成功" : "新增成功");
        xadmin.close();
      } else {
        fsoft.errorMsg(rs.msg);
      }
    });
    return false;
  });
  //菜单类型监听
  form.on('select(menu-menuType)', function (data) {
    rebulidActionUrl(data);
  });
});
/**
 * 根据菜单类型（按钮、菜单、目录），控制URL/图标是否显示
 * @param data
 * @returns
 */
function rebulidActionUrl(data) {
  $("#F_actionUrl").show();
  $("#F_icon").show();
  if (data.value != "1") {
    $("#F_actionUrl").hide();
    $("#actionUrl").val("");

  }
  if (data.value == "2" || data.value == "3") {
    $("#F_icon").hide();
    $("#icon").val("");
  }
}